package org.jeecg.modules.cshApp.service.impl;

import com.bestvike.linq.Linq;
import org.jeecg.modules.cshApp.entity.CshAreasEntity;
import org.jeecg.modules.cshApp.entity.CshCitiesEntity;
import org.jeecg.modules.cshApp.entity.CshProvincesEntity;
import org.jeecg.modules.cshApp.mapper.CshAdressMapper;
import org.jeecg.modules.cshApp.mapper.CshAreasMapper;
import org.jeecg.modules.cshApp.mapper.CshCitiesEntityMapper;
import org.jeecg.modules.cshApp.mapper.CshProvincesEntityMapper;
import org.jeecg.modules.cshApp.service.ICshAdressService;
import org.jeecg.modules.cshApp.service.ICshAreasService;
import org.jeecg.modules.cshApp.service.ICshCitiesEntityService;
import org.jeecg.modules.cshApp.service.ICshProvincesEntityService;
import org.jeecg.modules.cshApp.vo.CshAdressCityModule;
import org.jeecg.modules.cshApp.vo.CshAdressModule;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @Description: 地址管理
 * @Author: 许泽平
 * @Date: 2020-04-01
 * @Version: V1.0
 */
@Service
@Transactional
public class CshAdressServiceImpl implements ICshAdressService {

    @Resource
    private CshAdressMapper cshAdressMapper;

    @Resource
    private ICshAreasService cshAreasService;

    @Resource
    private ICshCitiesEntityService cshCitiesEntityService;

    @Resource
    private ICshProvincesEntityService cshProvincesEntityService;

    @Resource
    private CshProvincesEntityMapper cshProvincesEntityMapper;

    @Resource
    private CshCitiesEntityMapper cshCitiesEntityMapper;

    @Resource
    private CshAreasMapper cshAreasMapper;

    /**
     * 获取所有省份信息
     *
     * @return 返回所有省份信息
     */
    @Override
    public List<CshAdressModule> findAllProAndCityAndArea() {

        List<CshProvincesEntity> provinceList = cshProvincesEntityService.list();
        List<CshCitiesEntity> cityList = cshCitiesEntityService.list();
        List<CshAreasEntity> areaList = cshAreasService.list();

        List<CshAdressModule> list = new ArrayList<>();

        for (CshProvincesEntity cshProvincesEntity : provinceList) {
            CshAdressModule cshAdressModule = new CshAdressModule();
            List<CshCitiesEntity> cshCitiesEntities = Linq.of(cityList).where(x -> cshProvincesEntity.getProvinceId().equals(x.getProvinceId().toString())).toList();
            cshAdressModule.setCshProvincesEntity(cshProvincesEntity);
            List<CshAdressCityModule> cshAdressCityModules = new ArrayList<>();
            for (CshCitiesEntity cshCityEntity : cshCitiesEntities) {
                CshAdressCityModule cshAdressCityModule = new CshAdressCityModule();
                List<CshAreasEntity> cshAreasEntities = Linq.of(areaList).where(x -> cshCityEntity.getCityId().equals(x.getCityId())).toList();
                cshAdressCityModule.setCshCitiesEntity(cshCityEntity);
                cshAdressCityModule.setCshAreaList(cshAreasEntities);
                cshAdressCityModules.add(cshAdressCityModule);
            }
            cshAdressModule.setCshAdressCityModuleList(cshAdressCityModules);
            list.add(cshAdressModule);
        }

        return list;

    }

    /**
     * 根据省id获取省及其直辖市区
     *
     * @param provinceId 省id
     * @return 返回根据省id获取省及其直辖市区
     */
    @Override
    public CshAdressModule findFjAreas(String provinceId) {

        CshProvincesEntity cshProvincesEntity = cshProvincesEntityMapper.findByProvinceId(provinceId);
        CshAdressModule cshAdressModule = new CshAdressModule();
        List<CshCitiesEntity> cshCitiesEntityList = cshCitiesEntityMapper.findByProvinceId(provinceId);

        cshAdressModule.setCshProvincesEntity(cshProvincesEntity);
        List<CshAdressCityModule> cshAdressCityModules = new ArrayList<>();

        for(CshCitiesEntity cshCitiesEntity : cshCitiesEntityList){
            CshAdressCityModule cshAdressCityModule = new CshAdressCityModule();
            List<CshAreasEntity> cshAreaList = cshAreasMapper.findByCityId(cshCitiesEntity.getCityId());
            cshAdressCityModule.setCshCitiesEntity(cshCitiesEntity);
            cshAdressCityModule.setCshAreaList(cshAreaList);
            cshAdressCityModules.add(cshAdressCityModule);
        }

        cshAdressModule.setCshAdressCityModuleList(cshAdressCityModules);

        return cshAdressModule;
    }

    /**
     * 获取开通城市
     *
     * @return 获取开通城市
     */
    @Override
    public List<CshCitiesEntity> findByOpenCity() {
        return cshCitiesEntityMapper.findByOpenCity();
    }
}
